<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/4
 * Time: 11:15
 */

namespace Biz\model;


class ShopCategoryEntrance extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop';

    const _DATABASE_ = 'shop';

    const _TABLE_NAME = 'shop_category_entrance';

    const STATUS_NORMAL = 1; // 上架
    const STATUS_OFFLINE = 10; //下架
    const STATUS_DELETE = 0; // 删除

    public static function getCategory($time)
    {
        return self::select(['id', 'banner', 'link', 'link_dest', 'title'], [
            'AND' => [
                'start_time[<]' => $time,
                'status' => self::STATUS_NORMAL,
            ],
            'ORDER' => ['sort' => 'DESC', 'addtime' => 'DESC'],
            'LIMIT' => [0, 8]
        ]);
    }

    public static function getCategory_347($time)
    {
        return self::select(['id', 'banner', 'link', 'link_dest', 'title'], [
            'AND' => [
                'start_time[<]' => $time,
                'status' => self::STATUS_NORMAL,
            ],
            'ORDER' => ['sort' => 'DESC', 'addtime' => 'DESC'],
            'LIMIT' => [0, 10]
        ]);
    }

    public static function getCategoryEntranceInfoById($id)
    {

        return self::get('*', [
            'AND' => [
                'id' => $id,
                'status' => [self::STATUS_NORMAL, self::STATUS_OFFLINE],
            ]
        ]);
    }

    public static function getCategoryEntranceList($limit)
    {

        return self::select('*', [
            'AND' => [
                'status' => [self::STATUS_NORMAL, self::STATUS_OFFLINE],
            ],
            'ORDER' => ['sort' => 'DESC'],
            'LIMIT' => $limit,
        ]);
    }

    public static function getCategoryEntranceCount($order = [], $limit)
    {

        return self::count('*', [
            'AND' => [
                'status' => [self::STATUS_NORMAL, self::STATUS_OFFLINE],
            ],
        ]);
    }

    public static function addCategoryEntrance($data)
    {

        $data['status'] = self::STATUS_NORMAL;
        $data['addtime'] = date('Y-m-d H:i:s');

        if (self::insert($data)->rowcount()) {

            return self::id();
        }
    }

    public static function updateCategoryEntranceInfoById($id, $data)
    {

        $data['edittime'] = date('Y-m-d H:i:s');
        if (self::update($data, ['id' => $id])->rowcount()) {

            return $id;
        }
    }

    public static function changeStatus($id, $status)
    {
        if (self::update(['status' => $status], ['id' => $id])->rowcount()) {

            return $id;
        }
    }

    public static function checkWeight($weight, $id = '')
    {

        if (!empty($id)) {
            $where['AND']['id[!]'] = $id;
        }
        $where['AND']['sort'] = $weight;
        $where['AND']['status'] = [self::STATUS_NORMAL, self::STATUS_OFFLINE];

        return self::select('sort', $where);
    }

    public static function getCategoryEntrancePage()
    {

        return self::select('id', [
            'AND' => [
                'status' => [self::STATUS_NORMAL, self::STATUS_OFFLINE],
            ],
            'ORDER' => ['sort' => 'DESC'],
        ]);
    }

}